<?php
    include 'include/function.php';
    valid_user();
    if (get_magic_quotes_gpc())
        $_GET['contest_id'] = stripslashes($_GET['contest_id']);
    $contest_id = mysql_escape_string($_GET['contest_id']);
    valid_contest($contest_id);
    include 'include/header.php';
    $contest = get_contest_info($contest_id);
    $contest_title = $contest['title'];
?>
<title>Contest Standing</title>
    <div id="headline">
        <div class="wrapper">
            <h2>Contest - <?php echo $contest_title; ?></h2>
        </div>
    </div>    

<div id="pagebody">
	<div class="wrapper">
    <div class="notice">
<?php
	echo '<strong>Start Time:</strong>' . $contest['start_time'] . '&nbsp;&nbsp;';
	echo '<strong>End Time:</strong>' . $contest['end_time'] . '<br>';
	$start = strtotime($contest['start_time']);
	$end = strtotime($contest['end_time']);
	$current = time();
	echo '<strong>Current Time:</strong><span id="tm">';
    echo date("Y-m-d H:i:s", $current);
    echo '</span>&nbsp;&nbsp;';
	echo '<strong>Status:';
	if ($current >= $end)
		echo '<font color=green>Finished</font>';
	else if ($current >= $start)
		echo "<font color=red>Running</font>";
	else
		echo "<font color=green>Started at:&nbsp;{$contest['start_time']}</font>";
    echo "</strong>";
?>
    
    </div>
<?php //added by felix021 @ 2009-03-02 13:49
	$start = strtotime($contest['start_time']);
	$current = time();
  if($current > $start){
?>
    
<table class="center" id="contest_list">
    <thead>
        <tr>
            <th width="45">Rank</th>
            <th width="150">Nick Name</th>
            <th width="50">AC</th>
            <th width="100">Penalty</th>
<?php
    $problem = get_problem_info($contest_id);
	$problem_num = count($problem);
    $standing = get_standing($contest_id);
    $rows = count($standing);

    for ($j = 0; $j < $problem_num; $j++) {
        $cnt_ac[$j] = 0;
        $cnt_total[$j] = 0;
        for ($i = 0; $i < $rows; $i++) {
            if (!empty($standing[$i][$problem[$j]['as_problem']][0]))
                $cnt_ac[$j]++;
            $cnt_total[$j] += $standing[$i][$problem[$j]['as_problem']][1];
        }
        $cnt_total[$j] += $cnt_ac[$j];
    }

    for ($i = 0; $i < $problem_num; $i++)
		echo "<th width=\"55\"><a href=\"{$problem[$i]['url']}\">{$problem[$i]['as_problem']} <sup>{$cnt_ac[$i]}/{$cnt_total[$i]}</sup></a></th>";
	echo "</tr>";
    echo "</thead>";
    
    for ($i = 0; $i < $rows; $i++) {
        echo "<tr>";
        echo "<td>";
        echo $i + 1;
        echo "</td>";
        echo "<td>" . $standing[$i]['nickname'][0] . "</td>";
        echo "<td>" . $standing[$i]['ac_num'][0] . "</td>";
        echo "<td>" . $standing[$i]['penalty'][0] . "</td>";

        for ($j = 0; $j < $problem_num; $j++) {
            $t0 = $standing[$i][$problem[$j]['as_problem']][0];
            $t1 = $standing[$i][$problem[$j]['as_problem']][1];
            
            echo "<td>";
            if (empty($t0) && $t1 == 0)
                continue;
            if (empty($t0) && $t1 != 0)
                echo "-$t1";
            if (!empty($t0) && $t1 == 0)
                echo $t0;
            if (!empty($t0) && $t1 != 0)
                echo "$t0(-$t1)";
            echo "</td>";
        }
        echo "<tr>";
        echo "\n";
    }
?>
</tbody></table>

<?php //added by felix021 @ 2009-03-02 13:49
  }else{
    echo "<div class=\"notice\">Contest is not started yet, please try againt later.</div>";
  }
?>
  <br><div>

  </div><br>
  </div>

<script language="javascript">
var cstatus = <?php echo $current < $start ? 0 : 1; ?>;
var timestamp = <?php echo time(); ?> * 1000;
var starttime = <?php echo $start; ?> * 1000;
function update(){
    var y,m,d,g,i,s;
    timestamp+=1000;
    if (timestamp > starttime && cstatus == 0)
        window.location = window.location;
    var t = new Date(timestamp);
    y=t.getFullYear();
    m=t.getMonth()+1; if (m<10) m="0"+m;
    d=t.getDate(); if (d<10) d="0"+d;
    g=t.getHours(); if (g<10) g="0"+g;
    i=t.getMinutes(); if (i<10) i="0"+i;
    s=t.getSeconds(); if (s<10) s="0"+s;
    var str = y+"-"+m+"-"+d+" "+g+":"+i+":"+s;
    document.getElementById("tm").innerHTML = str;
}
setInterval("update()", 1000);
</script>
<?php
    include('include/footer.php');
?>
